我们一般看到的Matplotlib库的绘图风格是这样的:
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('Default Style')
plt.legend()
plt.show()
实际上Matplotlib可选的绘图风格有很多,我们可以通过plt.style.available
进行查看,通过plt.style.use()
可以运用某个风格,所含有的风格包括:
'Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10'
一共有26种之多,我们来一次性了解下这些风格:
plt.figure(figsize=(18,20))
i = 1
for style in plt.style.available:
plt.style.use(style)
plt.subplot(7,4,i)
plt.plot(x,y,label=style)
#plt.title(style)
plt.legend()
i = i+1
plt.show()
可以看到不同风格的图片在配色、线条、网格、标题等会有区别,同学们可以根据自己的偏好设置风格,让自己的图片与众不同。
当绘图中含有中文要素却未设置中文字体时,绘制的图片中的中文会被举行代替,如下图:
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.show()
这时我们只需要在绘图代码前设置中文格式即可
plt.rcParams['font.family']=['SimHei'] # 设置微软雅黑格式
plt.rcParams['axes.unicode_minus'] = False
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.show()
上面代码我们使用的是微软雅黑
字体,有的电脑可能没有这种字体,这时,就需要查看电脑中含有的中文字体包括哪些,然后引入进去。
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('Default Style')
plt.legend()
plt.savefig('images/mat0201.png')
plt.show()
print(len(plt.style.available))
26
plt.figure(figsize=(18,20))
i = 1
for style in plt.style.available:
plt.style.use(style)
plt.subplot(7,4,i)
plt.plot(x,y,label=style)
#plt.title(style)
plt.legend()
i = i+1
plt.savefig('images/mat0202.png')
plt.show()
No handles with labels found to put in legend.
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.savefig('images/mat0203.png')
plt.show()
D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 40664 missing from current font. font.set_text(s, 0.0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 35748 missing from current font. font.set_text(s, 0.0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 39118 missing from current font. font.set_text(s, 0.0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:238: RuntimeWarning: Glyph 26684 missing from current font. font.set_text(s, 0.0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:201: RuntimeWarning: Glyph 40664 missing from current font. font.set_text(s, 0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:201: RuntimeWarning: Glyph 35748 missing from current font. font.set_text(s, 0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:201: RuntimeWarning: Glyph 39118 missing from current font. font.set_text(s, 0, flags=flags) D:\software_install\anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:201: RuntimeWarning: Glyph 26684 missing from current font. font.set_text(s, 0, flags=flags)
plt.rcParams['font.family']=['SimHei'] # 设置微软雅黑格式
plt.rcParams['axes.unicode_minus'] = False
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
import matplotlib.pyplot as plt
import numpy as np
# 绘制y=sin(x)图像
x = np.linspace(-2*np.pi,2*np.pi,1000)
y = np.sin(x)
plt.plot(x,y,label='y=sin(x)')
plt.title('默认风格')
plt.legend()
plt.savefig('images/mat0205.png')
plt.show()